﻿using System;
using org.se.fs.presenter.IView.Profile;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.Task.Template;

namespace org.se.fs.presenter.View.Profile
{
    public class ClientSubMenu : Template.SubMenu
    {
        private readonly IClientSubMenu _view;

        public ClientSubMenu(IClientSubMenu view) : this(view, new Task.Context.Client()) { }

        public ClientSubMenu(IClientSubMenu view, Crud task)
        {
            _view = view;
            Task = task;
            FormView = _view;
            Name = "Client";
        }

        protected override void SetUi()
        {
            _view.Surname = Data["last_name"].ToString();
            _view.Firstname = Data["first_name"].ToString();
            _view.Midname = Data["middle_name"].ToString();
            _view.Gender = Data["gender"].ToString();
            _view.Address = Data["address"].ToString();
            _view.Birthdate = Convert.ToDateTime(Data["birth_date"].ToString());
            _view.Email = Data["email"].ToString();
            _view.Mobile = Data["mobile_num"].ToString();
            _view.Tel = Data["tel_num"].ToString();
        }

        protected override void FillData()
        {
            Data["last_name"] = _view.Surname;
            Data["first_name"] = _view.Firstname;
            Data["middle_name"] = _view.Midname;
            Data["gender"] = _view.Gender;
            Data["address"] = _view.Address;
            Data["birth_date"] = _view.Birthdate;
            Data["email"] = _view.Email;
            Data["mobile_num"] = _view.Mobile;
            Data["tel_num"] = _view.Tel;
            Data["user_create_id"] = ToUInt64(Config.User["id"]);
            //Data["user_last_name"] = Config.User.Rows[0]["last_name"].ToString();
            //Data["user_first_name"] = Config.User.Rows[0]["first_name"].ToString();
            //Data["user_middle_name"] = Config.User.Rows[0]["middle_name"].ToString();
            //Data["user_name"] = Config.User.Rows[0]["user_name"].ToString();
        }

        protected override bool Validate()
        {
            if (string.IsNullOrEmpty(_view.Surname.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty surname.") });
                _view.FocusSurname();
                return false;
            }

            if (string.IsNullOrEmpty(_view.Firstname.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty firstname.") });
                _view.FocusFirstname();
                return false;
            }

            if (string.IsNullOrEmpty(_view.Midname.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty midname.") });
                _view.FocusMidname();
                return false;
            }

            if (string.IsNullOrEmpty(_view.Address.Trim()))
            {
                TriggerValidate(new FormEvent(false) { Exception = new Exception("Invalid empty address.") });
                _view.FocusAddress();
                return false;
            }

            TriggerValidate(new FormEvent(true));
            return true;
        }
    }
}
